//
//  QuestionXMLParser.m
//  MathMasterMain
//
//  Created by Jimmy Hahm on 10-03-27.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "QuestionXMLParser.h"
NSString* const QUESTIONS = @"Questions";
NSString* const QUESTION = @"Question";
NSString* const QTYPE = @"QType";
NSString* const OPTION1 = @"Option1";
NSString* const OPTION2 = @"Option2";
NSString* const OPTION3 = @"Option3";
NSString* const OPTION4 = @"Option4";
NSString* const ANSWER = @"Answer";
NSString* const IMGLoca = @"IMGLocation";


@implementation QuestionXMLParser
- (void) parser: (NSXMLParser*) parser 
didStartElement: (NSString*) elementName 
   namespaceURI: (NSString*) namespaceURI 
  qualifiedName: (NSString*) qualifiedName 
	 attributes: (NSDictionary*) attributeDict
{
    if ( TRUE == [ elementName isEqualToString: QUESTIONS ] )
    {
		Questions* Q = [ Questions new ];
		[ _elementStack addObject: Q ];
    }
    else if (TRUE == [ elementName isEqualToString: QUESTION ] ||
			 TRUE == [ elementName isEqualToString: OPTION1 ] ||
			 TRUE == [ elementName isEqualToString: OPTION2 ] ||
			 TRUE == [ elementName isEqualToString: OPTION3 ] ||
			 TRUE == [ elementName isEqualToString: OPTION4 ] ||
			 TRUE == [ elementName isEqualToString: ANSWER ] ||
			 TRUE == [ elementName isEqualToString: IMGLoca ])
    {
		NSString* element = [ NSString stringWithString: elementName ];
		[ _elementStack addObject: element ];
    }
}

- (void) parser: (NSXMLParser*) parser
foundCharacters: (NSString*) string
{
    NSCharacterSet* characterSet = [ NSCharacterSet 
									whitespaceAndNewlineCharacterSet ];
    NSString* trimmedValue = [ string stringByTrimmingCharactersInSet:
							  characterSet ];
    if ( 0 == [ trimmedValue length ] )
    {
		return;
    }
	
    id element = [ _elementStack lastObject ];
    if ( TRUE == [ element isKindOfClass: [ NSString class ] ] )
    {
		NSUInteger count = [ _elementStack count ];
		NSUInteger index = count - 2;
		id parentElement = [ _elementStack objectAtIndex: index ];
		
		if ( TRUE == [ element isEqualToString: QUESTION ] )
		{
			Questions* Q = (Questions*)parentElement;
			[ Q	setQuestion: trimmedValue ];
		}
		else if ( TRUE == [ element isEqualToString: OPTION1] )
		{
			Questions* Q = (Questions*)parentElement;
			[ Q	setOption1: trimmedValue ];
		}
		else if ( TRUE == [ element isEqualToString: OPTION2] )
		{
			Questions* Q = (Questions*)parentElement;
			[ Q	setOption2: trimmedValue ];
		}
		else if ( TRUE == [ element isEqualToString: OPTION3] )
		{
			Questions* Q = (Questions*)parentElement;
			[ Q	setOption3: trimmedValue ];
		}
		else if ( TRUE == [ element isEqualToString: OPTION4] )
		{
			Questions* Q = (Questions*)parentElement;
			[ Q	setOption4: trimmedValue ];
		}
		else if ( TRUE == [ element isEqualToString: ANSWER] )
		{
			Questions* Q = (Questions*)parentElement;
			[ Q	setAnswer: trimmedValue ];
		}
		else if ( TRUE == [ element isEqualToString: IMGLoca] )
		{
			Questions* Q = (Questions*)parentElement;
			[ Q	setIMGLocation: (NSURL *) trimmedValue ];
		}
    }
}

- (void) parser: (NSXMLParser*) parser 
  didEndElement: (NSString*) elementName
   namespaceURI: (NSString*) namespaceURI 
  qualifiedName: (NSString*) qName
{
    if ( TRUE == [ elementName isEqualToString: QUESTIONS ] )
    {
		Questions* Q = (Questions*)[ _elementStack lastObject ];
		[ _Q addObject: Q ];
		[ _elementStack removeLastObject ];
    }
    else if (TRUE == [ elementName isEqualToString: QUESTION ] ||
			 TRUE == [ elementName isEqualToString: OPTION1 ] ||
			 TRUE == [ elementName isEqualToString: OPTION2 ] ||
			 TRUE == [ elementName isEqualToString: OPTION3 ] ||
			 TRUE == [ elementName isEqualToString: OPTION4 ] ||
			 TRUE == [ elementName isEqualToString: ANSWER ] ||
			 TRUE == [ elementName isEqualToString: IMGLoca ])
    {
		[ _elementStack removeLastObject ];
    }
}

- (NSMutableArray*) parseContentOfUrl: (NSURL*) url //parse xml from url
{
    NSXMLParser* xmlParser = [ [ NSXMLParser alloc ] 
							  initWithContentsOfURL: url ];
    [ xmlParser setDelegate: self ];
	
    _Q = [ NSMutableArray arrayWithCapacity: 1024 ];
    _elementStack = [ NSMutableArray arrayWithCapacity: 1024 ];
	
    [ xmlParser parse ];
	
    return _Q;
}

- (NSMutableArray*) parseContentOfLocal: (NSString*) filename //parse xml file in resource group
{
	NSXMLParser *xmlnameparser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:filename]]];
	[ xmlnameparser setDelegate: self ];
	
    _Q = [ NSMutableArray arrayWithCapacity: 1024 ];
    _elementStack = [ NSMutableArray arrayWithCapacity: 1024 ];
	
    [ xmlnameparser parse ];
	
    return _Q;
}
@end
